package com.example.androidsx;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.webkit.WebView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.flexbox.FlexboxLayout;
import com.google.android.material.chip.Chip;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ProductDetailActivity extends AppCompatActivity {

    private Product product;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_product_detail);

        // 从Intent获取商品ID
        String productId = getIntent().getStringExtra("product_id");

        // 从数据库/API获取商品详情
        loadProductDetail(productId);

        // 初始化视图
        initViews();
    }

    public class Product {
        private String id;
        private String name;
        private double price;
        private String description;
        private float rating;
        private int ratingCount;
        private List<String> imageUrls;
        private List<ProductSpec> specs;
        private String detailHtml;

        // Getter 和 Setter 方法
        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }


        public void setDescription(String description) {
            this.description = description;
        }

        public float getRating() {
            return rating;
        }

        public void setRating(float rating) {
            this.rating = rating;
        }

        public int getRatingCount() {
            return ratingCount;
        }

        public void setRatingCount(int ratingCount) {
            this.ratingCount = ratingCount;
        }

        public List<String> getImageUrls() {
            return imageUrls;
        }

        public void setImageUrls(List<String> imageUrls) {
            this.imageUrls = imageUrls;
        }

        public void setSpecs(List<ProductSpec> specs) {
            this.specs = specs;
        }

        public String getDetailHtml() {
            return detailHtml;
        }

        public void setDetailHtml(String detailHtml) {
            this.detailHtml = detailHtml;
        }
    }

    public class ProductSpec {
        private String name;

    }

    private void loadProductDetail(String productId) {
        // 模拟数据
        product = new Product();
        product.setId(productId);
        product.setName("高端智能手机");
        product.setPrice(5999.00);
        product.setRating(4.5f);
        product.setRatingCount(1250);
        product.setDescription("这是一款高端智能手机");

        List<String> images = new ArrayList<>();
        images.add("https://example.com/product1.jpg");
        images.add("https://example.com/product2.jpg");
        product.setImageUrls(images);

        List<ProductSpec> specs = new ArrayList<>();

        product.setSpecs(specs);
        product.setDetailHtml("<p>这是商品的详细HTML描述</p>");
    }

    private void initViews() {
        // 设置商品基本信息
        TextView nameView = findViewById(R.id.productName);
        TextView priceView = findViewById(R.id.productPrice);
        RatingBar ratingBar = findViewById(R.id.productRating);
        TextView ratingCountView = findViewById(R.id.ratingCount);

        nameView.setText(product.getName());
        priceView.setText(String.format("¥%.2f", product.getPrice()));
        ratingBar.setRating(product.getRating());
        ratingCountView.setText(String.format("(%d评价)", product.getRatingCount()));

        // 设置图片轮播
        initImagePager();

        // 设置商品详情
        WebView webView = findViewById(R.id.productDetailWebView);
        webView.loadDataWithBaseURL(null, product.getDetailHtml(), "text/html", "UTF-8", null);

        // 设置按钮点击事件
        findViewById(R.id.addToCartBtn).setOnClickListener(v -> addToCart());
        findViewById(R.id.buyNowBtn).setOnClickListener(v -> buyNow());
    }

    private void initImagePager() {
        ViewPager2 viewPager = findViewById(R.id.productImagePager);
        TabLayout tabLayout = findViewById(R.id.imageIndicator);

        ProductImageAdapter adapter = new ProductImageAdapter(product.getImageUrls());
        viewPager.setAdapter(adapter);

        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {}).attach();
    }



    private void addToCart() {
        // 添加到购物车逻辑
        Toast.makeText(this, "已添加到购物车", Toast.LENGTH_SHORT).show();
    }

    private void buyNow() {
        // 立即购买逻辑
        Intent intent = new Intent(this, CheckoutActivity.class);
        intent.putExtra("product_id", product.getId());
        startActivity(intent);
    }
}
